import Cesium, { Rectangle } from '../Ces/Cesium';
import WeLengthways from './WeLengthways';
import WeDisplayParameter from './WeDisplayParameter';

// 定义参数类型
interface WeLengthwaysInRectOptions {
  rectangle: Rectangle;
  extrudedHeight: number;
  scaleZ: number;
  display: boolean;
  showGrid?: boolean;
}

class WeLengthwaysInRect {
  private rectangle: Rectangle;
  private extrudedHeight: number;
  private _scaleZ: number;
  private _display: boolean;
  private _lengthwaysEW?: WeLengthways;
  private _lengthwaysNS?: WeLengthways;
  private _sectPositionX: number;
  private _sectPositionY: number;

  constructor(options: WeLengthwaysInRectOptions) {
    this.rectangle = options.rectangle;
    this.extrudedHeight = options.extrudedHeight;
    this.scaleZ = options.scaleZ;
    this._display = options.display;
    this.sectPositionX = WeDisplayParameter.Lengthways.sectPositionX;
    this.sectPositionY = WeDisplayParameter.Lengthways.sectPositionY;
  }

  get sectPositionX(): number {
    return this._sectPositionX;
  }

  set sectPositionX(value: number) {
    if (value >= 0.0 && value <= 1.0) {
      this._sectPositionX = value;
      const rectangle = this.rectangle;
      const longitude = rectangle.west + rectangle.width * value;
      const positions = [
        longitude,
        rectangle.south,
        longitude,
        rectangle.north,
      ];

      if (!Cesium.defined(this._lengthwaysEW)) {
        this._lengthwaysEW = this.create({
          positions,
        });
        this._lengthwaysEW.weUniform.Properties.localExtent = rectangle;
      } else {
        this._lengthwaysEW.positions = positions;
      }
      this._lengthwaysEW.sumarize = value === 0.0 || value === 1.0 ? 1.0 : 0.0;
    }
  }

  // sectPositionY getter and setter
  get sectPositionY(): number {
    return this._sectPositionY;
  }

  set sectPositionY(value: number) {
    if (value >= 0.0 && value <= 1.0) {
      this._sectPositionY = value;
      const rectangle = this.rectangle;
      const latitude = rectangle.south + rectangle.height * value;
      const positions = [
        rectangle.west,
        latitude,
        rectangle.east,
        latitude,
      ];

      if (!Cesium.defined(this._lengthwaysNS)) {
        this._lengthwaysNS = this.create({
          positions,
        });
        this._lengthwaysNS.weUniform.Properties.localExtent = rectangle;
      } else {
        this._lengthwaysNS.positions = positions;
      }
      this._lengthwaysNS.sumarize = value === 0.0 || value === 1.0 ? 2.0 : 0.0;
    }
  }

  // scaleZ getter and setter
  get scaleZ(): number {
    return this._scaleZ;
  }

  set scaleZ(value: number) {
    if (this._scaleZ !== value) {
      if (Cesium.defined(this._lengthwaysEW)) {
        this._lengthwaysEW.scaleZ = value;
      }
      if (Cesium.defined(this._lengthwaysNS)) {
        this._lengthwaysNS.scaleZ = value;
      }
      this._scaleZ = value;
    }
  }

  // 创建 WeLengthways
  private create(options: { positions: number[] }): WeLengthways {
    return new WeLengthways({
      rectangle: this.rectangle,
      extrudedHeight: this.extrudedHeight,
      scaleZ: this.scaleZ,
      display: this._display,
      ...options,
    });
  }

  // isDestroyed 方法
  public isDestroyed(): boolean {
    return false;
  }

  // destroy 方法
  public destroy(): void {
    this._lengthwaysNS?.destroy()
    this._lengthwaysEW?.destroy()
  }

  // update 方法
  public update(frameState): void {
    if (frameState.passes.render) {
      if (WeDisplayParameter.Lengthways.sectPositionX !== this._sectPositionX) {
        this.sectPositionX = WeDisplayParameter.Lengthways.sectPositionX;
      }

      if (WeDisplayParameter.Lengthways.sectPositionY !== this._sectPositionY) {
        this.sectPositionY = WeDisplayParameter.Lengthways.sectPositionY;
      }
    }

    if (Cesium.defined(this._lengthwaysEW)) {
      this._lengthwaysEW.update(frameState);
    }
    if (Cesium.defined(this._lengthwaysNS)) {
      this._lengthwaysNS.update(frameState);
    }
  }
}

export default WeLengthwaysInRect;
